<template>
  <div class="app-container">
    <upload-excel-component
      class="upload"
      :on-success="handleSuccess"
      :before-upload="beforeUpload"
    />
  </div>
</template>
<script setup>
import { setexcel } from "@/http/api.js";
import UploadExcelComponent from "@/components/unload.vue";
import { useRouter } from "vue-router";
const router = useRouter();
const beforeUpload = (file) => {
  const isLt1M = file.size / 1024 / 1024 < 1;
  if (isLt1M) {
    return true;
  }
  this.$message({
    message: "Please do not upload files larger than 1m in size.",
    type: "warning",
  });
  return false;
};

const handleSuccess = async ({ results, header }) => {
  // results指表格中的内容
  // header指的是表头题目
  results.forEach((item) => {
    item.role = [{ title: item.role }];
    item.openTime = new Date().getTime();
  });
  results = results.filter((item) => item.username);
  console.log(results);
  const res = await setexcel(results);
  console.log(res);
  if (res.code == 200) {
    router.push("/user/manage");
  }
};
</script>
<style>
.upload {
  margin-top: 40px;
}
</style>